﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data.SqlClient;

namespace MonitorServer
{
    [DataContract]
    public class HtmlAction
    {
        [DataMember]
        public int ActionId { get; set; }
        [DataMember]
        public DateTime BeginTime { get; set; }
        [DataMember]
        public DateTime EndTime { get; set; }
        [DataMember]
        public string Location { get; set; }
        [DataMember]
        public string Url { get; set; }

        public int ElapsedTime
        {
            get
            {
                return (int)(EndTime - BeginTime).TotalMilliseconds;
            }
 
        }
        [DataMember]
        public int Scenario { get; set; }

        public DateTime BeginTimeFormat
        {
            get
            {
                return new DateTime(BeginTime.Year, BeginTime.Month, BeginTime.Day
                     , BeginTime.Hour, (BeginTime.Minute / 5) * 5, 0);
            }
        }

        protected string InsertSql
        {
            get
            {
                return string.Format(@"INSERT INTO [HtmlAction]
                                   ([BeginTime]
                                   ,[EndTime]
                                   ,[ElapsedTime]
                                   ,[BeginTimeFormat]
                                   ,[Location]
                                   ,[Url]
                                   ,[Scenario])
                                   VALUES(@BeginTime, @EndTime, @ElapsedTime, @BeginTimeFormat,@Location,@Url,@Scenario)");
            }
        }

        public void Insert()
        {
            SqlParameter[] sqlParameters = {
                                               new SqlParameter("@BeginTime", BeginTime),
                                               new SqlParameter("@EndTime", EndTime),
                                               new SqlParameter("@ElapsedTime", ElapsedTime),
                                               new SqlParameter("@BeginTimeFormat", BeginTimeFormat),
                                               new SqlParameter("@Location", Location),
                                               new SqlParameter("@Url", Url),
                                               new SqlParameter("@Scenario", Scenario),
                                           };
            Utility.SqlHelper.ExecuteNonQuery(InsertSql, sqlParameters);
        }
    }
}
